package consumer.util;

import java.io.IOException;
import java.util.Collection;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUtil {
	private static ObjectMapper mapper = new ObjectMapper();

    public JsonUtil() {
    }

    public static String beanToJson(Object object) {
        return beanToJson(object, false);
    }

    public static String beanToJson(Object object, boolean isFormat) {
        if (object == null) {
            return null;
        } else {
            try {
                return isFormat ? mapper.writerWithDefaultPrettyPrinter().writeValueAsString(object) : mapper.writeValueAsString(object);
            } catch (Exception var3) {
                throw new RuntimeException(var3);
            }
        }
    }

    public static <T> T jsonToBean(String json, Class<T> clazz) {
        try {
            return mapper.readValue(json, clazz);
        } catch (Exception var3) {
            throw new RuntimeException(var3);
        }
    }

    public static <T> T jsonToBean(String json, TypeReference<T> typeReference) {
        try {
            return mapper.readValue(json, typeReference);
        } catch (Exception var3) {
            throw new RuntimeException(var3);
        }
    }

    public static <T> T jsonToCollection(String json, Class<? extends Collection> collectionClass, Class<?> elementClass) {
        try {
            if (StringUtil.isNotEmpty(json)) {
                JavaType javaType = mapper.getTypeFactory().constructCollectionType(collectionClass, elementClass);
                return mapper.readValue(json, javaType);
            } else {
                return null;
            }
        } catch (Exception var4) {
            throw new RuntimeException(var4);
        }
    }

    public static String getValue(String jsonString, String nodeName) {
        try {
            JsonNode rootNode = mapper.readTree(jsonString);
            String val = getValue(rootNode, nodeName);
            return val;
        } catch (JsonProcessingException var5) {
            throw new RuntimeException(var5);
        } catch (IOException var6) {
            throw new RuntimeException(var6);
        }
    }

    public static JsonNode getRootNode(String json) {
        try {
            JsonNode rootNode = mapper.readTree(json);
            return rootNode;
        } catch (IOException var3) {
            throw new RuntimeException(var3);
        }
    }

    public static String getValue(JsonNode rootNode, String nodeName) {
        if (null != rootNode && !StringUtil.isBlank(nodeName)) {
            JsonNode node = rootNode.findValue(nodeName);
            if (null == node) {
                return null;
            } else {
                String val;
                if (node.isArray()) {
                    val = node.toString();
                } else {
                    val = node.asText();
                }
                return val;
            }
        } else {
            return null;
        }
    }
}
